/*****************************************************************************
*   ME32G030_timer.c:  source file for timer 
*		ver 1.0
*		
******************************************************************************/

#include "ME32G030.h"
#include "ME32G030_timer.h"
#include "ME32G030_sys.h"

extern uint32_t SystemCoreClock;
/*****************************************************************************
Function Name	BTIM_Init
Function Definition	void BTIM_Init(BTIM0_Type *ct, uint32_t tickpersecond)
Function Description	Initial basic timer
Input Parameters	*ct: pointer to BTIM type timer
tickpersecond: timer count frequency, less than system clock
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void BTIM_Init(BTIM0_Type *ct, uint32_t tickpersecond)
{
	switch ((uint32_t)ct)
	{
		case (uint32_t)BTIM0:
		SYSCON->SYSAHBCLKCTRL_b.BTIM0_CLK=1;
		SYSCON->PRESETCTRL_b.BTIM0_RST_N=0;
		SYSCON->PRESETCTRL_b.BTIM0_RST_N=1;
		break;
		case (uint32_t)BTIM1:
		SYSCON->SYSAHBCLKCTRL_b.BTIM1_CLK=1;
		SYSCON->PRESETCTRL_b.BTIM1_RST_N=0;
		SYSCON->PRESETCTRL_b.BTIM1_RST_N=1;
		break;
		case (uint32_t)BTIM2:
		SYSCON->SYSAHBCLKCTRL_b.BTIM2_CLK=1;
		SYSCON->PRESETCTRL_b.BTIM2_RST_N=0;
		SYSCON->PRESETCTRL_b.BTIM2_RST_N=1;
		break;
		case (uint32_t)BTIM3:
		SYSCON->SYSAHBCLKCTRL_b.BTIM3_CLK=1;
		SYSCON->PRESETCTRL_b.BTIM3_RST_N=0;
		SYSCON->PRESETCTRL_b.BTIM3_RST_N=1;
		break;
		default:
			return;
	}
	if (tickpersecond>SystemCoreClock)
			tickpersecond=SystemCoreClock;
	ct->PR_b.PRVAL=SystemCoreClock/tickpersecond;
	if (ct->PR_b.PRVAL>0)
		ct->PR_b.PRVAL-=1;
	ct->TCR_b.MOD=0;
	return;
}
/*****************************************************************************
Function Name	BTIM_ConfigMatch0
Function Definition	void BTIM_ConfigMatch0(BTIM0_Type *ct, uint16_t ticks0, uint8_t action)
Function Description	Setup Match to generate a match interrupt
Input Parameters	*ct: pointer to BTIM0~3
ticks: match value
action: combination of following action when counter reach match value
	TIM_MATCH_TRIGGER_INT 0x1
	TIM_MATCH_DO_NOTHING 0x0
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void BTIM_ConfigMatch0(BTIM0_Type *ct, uint16_t ticks, uint8_t action)
{
	ct->MR0_b.MATCH=ticks;
	if (ct->MR0_b.MATCH>1)
		ct->MR0_b.MATCH -=1;
	ct->MCR_b.MR0I = action;
	return;
}
/*****************************************************************************
Function Name	BTIM_ConfigMatch1
Function Definition	void BTIM_ConfigMatch1(BTIM0_Type *ct, uint16_t ticks, uint8_t action)
Function Description	Setup Match
Input Parameters	*ct: pointer to BTIM0~3
ticks: match value
action: combination of following action when counter reach match value
	TIM_MATCH_TRIGGER_INT 0x1
	TIM_MATCH_RESET_COUNTER 0x2
	TIM_MATCH_STOP_COUNTER 0x4
	TIM_MATCH_DO_NOTHING 0x0
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void BTIM_ConfigMatch1(BTIM0_Type *ct, uint16_t ticks, uint8_t action)
{
	ct->MR1_b.MATCH=ticks;
	if (ct->MR1_b.MATCH>1)
		ct->MR1_b.MATCH -=1;
	ct->MCR=(action<<1)|(ct->MCR&0x01);
	return;
}
/*****************************************************************************
Function Name	BTIM_SetPWMDT
Function Definition	void BTIM_SetPWMDT(BTIM0_Type *ct, uint16_t dt0, uint16_t dt1)
Function Description	Setup BTIM pwm out dead time on high/low line rise edge
Input Parameters	*ct: pointer to BTIM0~3
			dt0: dead time BTIM MAT0 at rise edge 
			dt1: dead time BTIM MAT0N at rise edge 
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void BTIM_SetPWMDT(BTIM0_Type *ct, uint16_t dt0, uint16_t dt1)
{
	ct->DT0=dt0;
	ct->DT1=dt1;
	return;
}
/*****************************************************************************
Function Name	BTIM_SetTimerCounter
Function Definition	void BTIM_SetTimerCounter(BTIM0_Type *ct, uint16_t tick)
Function Description	*ct: pointer to BTIM
tick: set current value
Input Parameters	No
Return Value	No
Condition	No
Function called	-

Last Chang Date: 2015/09/12			By:YL
*****************************************************************************/
void BTIM_SetTimerCounter(BTIM0_Type *ct, uint16_t tick)
{
	ct->PC_b.PCVAL=tick;
	return;
}
/*****************************************************************************
Function Name	BTIM_ResetTimerCounter
Function Definition	void BTIM_ResetTimerCounter(BTIM0_Type *ct)
Function Description	Reset Timer Counter value
Input Parameters	*ct: pointer to BTIM
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void BTIM_ResetTimerCounter(BTIM0_Type *ct)
{
	ct->PC_b.PCVAL=0;
	return;
}
/*****************************************************************************
Function Name	BTIM_GetTimerCounter
Function Definition	uint16_t BTIM_GetTimerCounter(BTIM0_Type *ct)
Function Description	Get current timer counter value
Input Parameters	*ct: pointer to BTIM
Return Value	Timer Counter value
Condition	No
Function called	-

*****************************************************************************/
uint16_t BTIM_GetTimerCounter(BTIM0_Type *ct)
{
	return ct->TC_b.TC;
}
/*****************************************************************************
Function Name	BTIM_ClearIntFlag
Function Definition	void BTIM_ClearIntFlag(BTIM0_Type *ct)
Function Description	Clear Match event interrupt
Input Parameters	*ct: pointer to BTIM/CTIM
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void BTIM_ClearIntFlag(BTIM0_Type *ct)
{
	ct->IR=0x03; //clear both MR0 and MR1 interrupt
	return;
}
/*****************************************************************************
Function Name	BTIM_ClearIntFlag
Function Definition	void BTIM_ClearIntFlag(BTIM0_Type *ct)
Function Description	Clear Match event interrupt
Input Parameters	*ct: pointer to BTIM
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void BTIM_EnableInt(BTIM0_Type *ct)
{
	ct->MCR_b.MR0I=1;

	return;
}
/*****************************************************************************
Function Name	BTIM_DisableInt
Function Definition	void BTIM_DisableInt(BTIM0_Type *ct)
Function Description	Disable Match event interrupt
Input Parameters	*ct: pointer to BTIM
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void BTIM_DisableInt(BTIM0_Type *ct)
{
	ct->MCR_b.MR0I=0;

	return;	
}
/*****************************************************************************
Function Name	CTIM_Init
Function Definition	void CTIM_Init(CTIM0_Type *ct, uint32_t tickpersecond)
Function Description	Initial timer
Input Parameters	*ct: pointer to CTIM
tickpersecond: timer count frequency, less than system clock
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_Init(CTIM0_Type *ct, uint32_t tickpersecond)
{
	switch ((uint32_t)ct)
	{
		case (uint32_t)CTIM0:
		SYSCON->SYSAHBCLKCTRL_b.CTIM0_CLK=1;
		SYSCON->PRESETCTRL_b.CTIM0_RST_N=0;
		SYSCON->PRESETCTRL_b.CTIM0_RST_N=1;
		break;
		case (uint32_t)CTIM1:
		SYSCON->SYSAHBCLKCTRL_b.CTIM0_CLK=1;
		SYSCON->PRESETCTRL_b.CTIM1_RST_N=0;
		SYSCON->PRESETCTRL_b.CTIM1_RST_N=1;
		break;
		default:
			return;
	}
	if (tickpersecond>SystemCoreClock)
			tickpersecond=SystemCoreClock;
	ct->PR_b.PCVAL=SystemCoreClock/tickpersecond;
	if (ct->PR_b.PCVAL>0)
		ct->PR_b.PCVAL-=1;	
	return;
}
/*****************************************************************************
Function Name	CTIM_SetCTType
Function Definition	void CTIM_SetCTType(CTIM0_Type *ct, uint8_t mode)
Function Description	Initial timer/counter mode
Input Parameters	*ct: pointer to CTIM
mode: counter/timer mode
			000 	Timer Mode, every PCLK
			001 	Counter Mode, Primary select signal rise edge
			010 	Counter Mode, Primary select signal fall edge 
			011 	Edge Count Mode  
			100	Quadrature mode
			101	Trigger count mode.
			110	Signed count mode.
			111	Gate count mode.
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_SetCTType(CTIM0_Type *ct, uint8_t mode)
{
	ct->CTCR_b.CTM=mode;
	return;
}
/*****************************************************************************
Function Name	CTIM_SelectTimerClearSignal
Function Definition	void CTIM_SelectTimerClearSignal(CTIM0_Type *ct, uint8_t capsignal, uint8_t edge)
Function Description	Select capture event to clear timer
Input Parameters	*ct: pointer to CTIM
capsignal: capture signal
					0, CAP0
					1, CAP1
					2, CAP2
					4, CAP3
					5, Other Timer CAP0
					6, Other Timer CAP1
					7, Other Timer CAP2
					8, Other Timer CAP3
Edge: RISE_EDGE/FALL_EDGE
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_SelectTimerClearSignal(CTIM0_Type *ct, uint8_t capsignal, uint8_t edge)
{
	//set counter clear singal
	ct->CTCR_b.SELCC=capsignal<<1;
	if (edge==FALL_EDGE)
			ct->CTCR_b.SELCC++;
	//enable counter clear action
	ct->CTCR_b.ENCC=1;
	return;
}	
/*****************************************************************************
Function Name	CTIM_SelectPriSrc
Function Definition	void CTIM_SelectPriSrc(CTIM0_Type *ct, uint8_t clksrc)
Function Description	Initial basic timer
Input Parameters	*ct: pointer to CTIM
clksrc: clock source
				0000	CAP0 pin
				0001	CAP1 pin
				0010	CAP2 pin
				0011	CAP3 pin
				0100	PWM Event
				0101	CTIM_MAT0
				0110	Reserved
				0111	Reserved.
				1xxx	Prescale counter
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_SelectPriSrc(CTIM0_Type *ct, uint8_t clksrc)
{
	ct->CTCR_b.PRISEL=clksrc;
	return;
}
/*****************************************************************************
Function Name	CTIM_SelectPriSrc
Function Definition	void CTIM_SelectSecSrc (CTIM0_Type *ct, uint8_t clksrc, uint8_t polarity)
Function Description	Initial basic timer
Input Parameters	*ct: pointer to CTIM
clksrc: clock source
				0000	CAP0 pin
				0001	CAP1 pin
				0010	CAP2 pin
				0011	CAP3 pin
				0100	PWM Event
				0101	CTIM_MAT0./ TIM7_MAT0
				0110	Reserved
				0111	Reserved.
				1xxx	Prescale counter
polarity: inverse clock source

Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_SelectSecSrc (CTIM0_Type *ct, uint8_t clksrc, uint8_t polarity)
{
	ct->CTCR_b.SECSEL=clksrc;
	ct->CTCR_b.IPS=polarity;
	return;
}
/*****************************************************************************
Function Name	CTIM_ConfigMatch0
Function Definition	void CTIM_ConfigMatch0(CTIM0_Type *ct, uint16_t ticks, uint8_t internalaction, uint8_t externalaction)
Function Description	Setup timer match 0 and internal action when match even occurs
Input Parameters	*ct: pointer to CTIM
ticks: match 0 value
internalaction: combination of following action when counter reach match value
               TIM_MATCH_TRIGGER_INT 0x1
               TIM_MATCH_RESET_COUNTER 0x2
               TIM_MATCH_STOP_COUNTER 0x4
               TIM_MATCH_DO_NOTHING 0x0 
externalaction: one of following action when counter reach match value
               TIM_MATCH_OUT_DO_NOTHING 0x0
               TIM_MATCH_OUT_RESET 0x1
               TIM_MATCH_OUT_SET	 0x2
               TIM_MATCH_OUT_TRIGGER_PLUS	 0x3

Return Value	No
Condition	No
Function called	-

Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_ConfigMatch0(CTIM0_Type *ct, uint16_t ticks, uint8_t internalaction, uint8_t externalaction)
{
	ct->MR0_b.MATCH=ticks-1;
	
	ct->MCR=(ct->MCR&(~0x7))|(internalaction&0x7);
	
	ct->EMR_b.EMC0=externalaction;
	ct->EMR_b.EM0=1;	
	return;
}
/*****************************************************************************
Function Name	CTIM_ConfigMatch1
Function Definition	void CTIM_ ConfigMatch1(CTIM0_Type *ct, uint16_t ticks, uint8_t internalaction, uint8_t externalaction)
Function Description	Setup timer match 1 and internal action and CTIM_MAT1/ CTIM_MAT1 output external action when match even occurs
Input Parameters	*ct: pointer to CTIM
ticks: match 1 value
internalaction: combination of following action when counter reach match value
			TIM_MATCH_TRIGGER_INT 0x1
			TIM_MATCH_RESET_COUNTER 0x2
			TIM_MATCH_STOP_COUNTER 0x4
			TIM_MATCH_DO_NOTHING 0x0 
externalaction: one of following action when counter reach match value
			TIM_MATCH_OUT_DO_NOTHING 0x0
			TIM_MATCH_OUT_RESET 0x1
			TIM_MATCH_OUT_SET	 0x2
			TIM_MATCH_OUT_TRIGGER_PLUS	 0x3
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_ConfigMatch1(CTIM0_Type *ct, uint16_t ticks, uint8_t internalaction, uint8_t externalaction)
{
	ct->MR1_b.MATCH=ticks-1;
	ct->MCR=(ct->MCR&(~(0x7<<3)))|((internalaction&0x7)<<3);

	ct->EMR_b.EMC1=externalaction;
	ct->EMR_b.EM1=1;	
	return;
}	
/*****************************************************************************
Function Name	CTIM_ConfigMatch2
Function Definition	void CTIM_ConfigMatch2(CTIM0_Type *ct, uint16_t ticks, uint8_t internalaction, uint8_t externalaction)
Function Description	Setup timer match 2 and internal action and CTIM_MAT2/ CTIM_MAT2 output external action when match even occurs
Input Parameters	*ct: pointer to CTIM
ticks: match 2 value
internalaction: combination of following action when counter reach match value
		TIM_MATCH_TRIGGER_INT 0x1
		TIM_MATCH_RESET_COUNTER 0x2
		TIM_MATCH_STOP_COUNTER 0x4
		TIM_MATCH_DO_NOTHING 0x0 
externalaction: one of following action when counter reach match value
		TIM_MATCH_OUT_DO_NOTHING 0x0
		TIM_MATCH_OUT_RESET 0x1
		TIM_MATCH_OUT_SET	 0x2
		TIM_MATCH_OUT_TRIGGER_PLUS	 0x3
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_ConfigMatch2(CTIM0_Type *ct, uint16_t ticks, uint8_t internalaction, uint8_t externalaction)
{
	ct->MR2_b.MATCH=ticks-1;
	ct->MCR=(ct->MCR&(~(0x7<<6)))|((internalaction&0x7)<<6);
	
	ct->EMR_b.EMC2=externalaction;
	ct->EMR_b.EM2=1;		
	return;
}
/*****************************************************************************
Function Name	CTIM_ConfigMatch3
Function Definition	void CTIM_ConfigMatch3(CTIM0_Type *ct, uint16_t ticks, uint8_t internalaction, uint8_t externalaction)
Function Description	Setup timer match 3 and internal action and CTIM_MAT3/ CTIM_MAT3 output external action when match even occurs
Input Parameters	*ct: pointer to CTIM
ticks: match 3 value
internalaction: combination of following action when counter reach match value
		TIM_MATCH_TRIGGER_INT 0x1
		TIM_MATCH_RESET_COUNTER 0x2
		TIM_MATCH_STOP_COUNTER 0x4
		TIM_MATCH_DO_NOTHING 0x0 
externalaction: one of following action when counter reach match value
		TIM_MATCH_OUT_DO_NOTHING 0x0
		TIM_MATCH_OUT_RESET 0x1
		TIM_MATCH_OUT_SET	 0x2
		TIM_MATCH_OUT_TRIGGER_PLUS	 0x3
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_ConfigMatch3(CTIM0_Type *ct, uint16_t ticks, uint8_t internalaction, uint8_t externalaction)
{
	ct->MR3_b.MATCH=ticks-1;
	ct->MCR=(ct->MCR&(~(0x7<<9)))|((internalaction&0x7)<<9);
	
	ct->EMR_b.EMC3=externalaction;
	ct->EMR_b.EM3=1;			
	return;
}

/*****************************************************************************
Function Name	CTIM_ConfigCapture0
Function Definition	void CTIM_ConfigCapture0(CTIM0_Type *ct, uint8_t edge, uint8_t intreq,uint8_t filter)
Function Description	Set up Capture 0 
Input Parameters	*ct: pointer to CTIM
edge: CAP0 signal edge, RISE_EDGE/FALL_EDGE/BOTH_EDGE. Deifned in sys.h
intreq: interrupt request
			TIM_CAPTURE_DO_NOTHING
			TIM_CAPTURE_TRIGGER_INT
filter: filter, PCLK number
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_ConfigCapture0(CTIM0_Type *ct, uint8_t edge, uint8_t intreq,uint8_t filter)
{
	switch(edge)
	{
		case RISE_EDGE:
				ct->CCR_b.CAP0RE=1;
				ct->CCR_b.CAP0FE=0;
			break;
		case FALL_EDGE:
				ct->CCR_b.CAP0RE=0;
				ct->CCR_b.CAP0FE=1;
			break;
		case BOTH_EDGE:
				ct->CCR_b.CAP0RE=1;
				ct->CCR_b.CAP0FE=1;
			break;
		default:
			break;
	}
	ct->CAPFILTER0 =filter;
	if (intreq==TIM_CAPTURE_TRIGGER_INT)
		ct->CCR_b.CAP0I=1;
	else
		ct->CCR_b.CAP0I=0;
	return;
}
/*****************************************************************************
Function Name	CTIM_ConfigCapture1
Function Definition	void CTIM_ConfigCapture1 (CTIM0_Type *ct, uint8_t edge, uint8_t intreq)
Function Description	Set up Capture 1 
Input Parameters	*ct: pointer to CTIM
edge: CAP0 signal edge, RISE_EDGE/FALL_EDGE/BOTH_EDGE. Deifned in sys.h
intreq: interrupt request
			TIM_CAPTURE_DO_NOTHING
			TIM_CAPTURE_TRIGGER_INT
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_ConfigCapture1(CTIM0_Type *ct, uint8_t edge, uint8_t intreq,uint8_t filter)
{
	switch(edge)
	{
		case RISE_EDGE:
				ct->CCR_b.CAP1RE=1;
				ct->CCR_b.CAP1FE=0;
			break;
		case FALL_EDGE:
				ct->CCR_b.CAP1RE=0;
				ct->CCR_b.CAP1FE=1;
			break;
		case BOTH_EDGE:
				ct->CCR_b.CAP1RE=1;
				ct->CCR_b.CAP1FE=1;
			break;
		default:
			break;
	}
	ct->CAPFILTER1 =filter;
	if (intreq==TIM_CAPTURE_TRIGGER_INT)
		ct->CCR_b.CAP1I=1;
	else
		ct->CCR_b.CAP1I=0;
	return;
}
/*****************************************************************************
Function Name	CTIM_ConfigCapture2
Function Definition	void CTIM_ConfigCapture2 (CTIM0_Type *ct, uint8_t edge, uint8_t intreq)
Function Description	Set up Capture 2 
Input Parameters	*ct: pointer to CTIM
edge: CAP0 signal edge, RISE_EDGE/FALL_EDGE/BOTH_EDGE. Deifned in sys.h
intreq: interrupt request
			TIM_CAPTURE_DO_NOTHING
			TIM_CAPTURE_TRIGGER_INT
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_ConfigCapture2(CTIM0_Type *ct, uint8_t edge, uint8_t intreq,uint8_t filter)
{
	switch(edge)
	{
		case RISE_EDGE:
				ct->CCR_b.CAP2RE=1;
				ct->CCR_b.CAP2FE=0;
			break;
		case FALL_EDGE:
				ct->CCR_b.CAP2RE=0;
				ct->CCR_b.CAP2FE=1;
			break;
		case BOTH_EDGE:
				ct->CCR_b.CAP2RE=1;
				ct->CCR_b.CAP2FE=1;
			break;
		default:
			break;
	}
	ct->CAPFILTER2 =filter;
	if (intreq==TIM_CAPTURE_TRIGGER_INT)
		ct->CCR_b.CAP2I=1;
	else
		ct->CCR_b.CAP2I=0;
	return;
}
/*****************************************************************************
Function Name	CTIM_ConfigCapture3
Function Definition	void CTIM_ConfigCapture3 (CTIM0_Type *ct, uint8_t edge, uint8_t intreq)
Function Description	Set up Capture 3 
Input Parameters	*ct: pointer to CTIM
edge: CAP0 signal edge, RISE_EDGE/FALL_EDGE/BOTH_EDGE. Deifned in sys.h
intreq: interrupt request
			TIM_CAPTURE_DO_NOTHING
			TIM_CAPTURE_TRIGGER_INT
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_ConfigCapture3(CTIM0_Type *ct, uint8_t edge, uint8_t intreq,uint8_t filter)
{
	switch(edge)
	{
		case RISE_EDGE:
				ct->CCR_b.CAP3RE=1;
				ct->CCR_b.CAP3FE=0;
			break;
		case FALL_EDGE:
				ct->CCR_b.CAP3RE=0;
				ct->CCR_b.CAP3FE=1;
			break;
		case BOTH_EDGE:
				ct->CCR_b.CAP3RE=1;
				ct->CCR_b.CAP3FE=1;
			break;
		default:
			break;
	}
	ct->CAPFILTER3 =filter;
	if (intreq==TIM_CAPTURE_TRIGGER_INT)
		ct->CCR_b.CAP3I=1;
	else
		ct->CCR_b.CAP3I=0;
	return;
}
/*****************************************************************************
Function Name	CTIM_GetCapture0Value
Function Definition	void CTIM_GetCapture0Value(CTIM0_Type *ct)
Function Description	Return timer Capture 0 register value
Input Parameters	*ct: pointer to CTIM
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
uint16_t CTIM_GetCapture0Value (CTIM0_Type *ct)
{
	return ct->CR0_b.CAP;
}
/*****************************************************************************
Function Name	CTIM_GetCapture1Value
Function Definition	void CTIM_GetCapture1Value(CTIM0_Type *ct)
Function Description	Return timer Capture 1 register value
Input Parameters	*ct: pointer to CTIM
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
uint16_t CTIM_GetCapture1Value (CTIM0_Type *ct)
{
	return ct->CR1_b.CAP;
}
/*****************************************************************************
Function Name	CTIM_GetCapture2Value
Function Definition	void CTIM_GetCapture2Value(CTIM0_Type *ct)
Function Description	Return timer Capture 2 register value
Input Parameters	*ct: pointer to CTIM
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
uint16_t CTIM_GetCapture2Value (CTIM0_Type *ct)
{
	return ct->CR2_b.CAP;
}
/*****************************************************************************
Function Name	CTIM_GetCapture3Value
Function Definition	void CTIM_GetCapture3Value(CTIM0_Type *ct)
Function Description	Return timer Capture 3 register value
Input Parameters	*ct: pointer to CTIM
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
uint16_t CTIM_GetCapture3Value (CTIM0_Type *ct)
{
	return ct->CR3_b.CAP;
}
/*****************************************************************************
Function Name	CTIM_SetTimerCounter
Function Definition	void CTIM_SetTimerCounter(CTIM0_Type *ct, uint16_t ticks)
Function Description	Set timer counter current value
Input Parameters	*ct: pointer to CTIM
tick: value to be set to current counter
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_SetTimerCounter(CTIM0_Type *ct, uint16_t ticks)
{
	ct->TC_b.TC=ticks;
	return;
}
/*****************************************************************************
Function Name	CTIM_ResetTimerCounter
Function Definition	void CTIM_ResetTimerCounter(CTIM0_Type *ct)
Function Description	Reset timer counter current value
Input Parameters	*ct: pointer to CTIM
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_ResetTimerCounter(CTIM0_Type *ct)
{
	ct->TCR_b.CRST=1;
	return;
}
/*****************************************************************************
Function Name	CTIM_GetTimerCounter
Function Definition	uint16_tCTIM_GetTimerCounter (CTIM0_Type *ct)
Function Description	Get timer counter current value
Input Parameters	*ct: pointer to CTIM
Return Value	Current counter value
Condition	No
Function called	-

*****************************************************************************/
uint16_t CTIM_GetTimerCounter (CTIM0_Type *ct)
{
	return ct->TC_b.TC;
}
/*****************************************************************************
Function Name	CTIM_ClearIntFlag
Function Definition	void CTIM_ClearIntFlag(CTIM0_Type *ct)
Function Description	Clear interrupt
Input Parameters	*ct: pointer to CTIM
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_ClearIntFlag(CTIM0_Type *ct)
{
	ct->IR=0xFF;
	return;
}
/*****************************************************************************
Function Name	CTIM_EnableInt
Function Definition	void CTIM_EnableInt(CTIM0_Type *ct, uint8_t intbit)
Function Description	Enable event interrupt
Input Parameters	*ct: pointer to CTIM
intbit: bit0~7 related match0~match3,capture0~capture3
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_EnableInt(CTIM0_Type *ct, uint8_t intbit)
{
		ct->MCR_b.MR0I |= (intbit&1);
		ct->MCR_b.MR1I |= ((intbit>>1)&1);
		ct->MCR_b.MR2I |= ((intbit>>2)&1);
		ct->MCR_b.MR3I |= ((intbit>>3)&1);
		ct->CCR_b.CAP0I |= ((intbit>>4)&1);
		ct->CCR_b.CAP1I |= ((intbit>>5)&1);
		ct->CCR_b.CAP2I |= ((intbit>>6)&1);
		ct->CCR_b.CAP3I |= ((intbit>>7)&1);
}
/*****************************************************************************
Function Name	CTIM_DisableInt
Function Definition	void CTIM_DisableInt(CTIM0_Type *ct, uint8_t intbit)
Function Description	Disable event interrupt
Input Parameters	*ct: pointer to CTIM
intbit: bit0~7 related match0~match3,capture0~capture3
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_DisableInt(CTIM0_Type *ct, uint8_t intbit)
{
		ct->MCR_b.MR0I &= ~(intbit&1);
		ct->MCR_b.MR1I &= ~((intbit>>1)&1);
		ct->MCR_b.MR2I &= ~((intbit>>2)&1);
		ct->MCR_b.MR3I &= ~((intbit>>3)&1);
		ct->CCR_b.CAP0I &= ~((intbit>>4)&1);
		ct->CCR_b.CAP1I &= ~((intbit>>5)&1);
		ct->CCR_b.CAP2I &= ~((intbit>>6)&1);
		ct->CCR_b.CAP3I &= ~((intbit>>7)&1);
}
/*****************************************************************************
Function Name	CTIM_GetIntStatus
Function Definition	uint8_t CTIM_GetIntStatus(CTIM0_Type *ct)
Function Description	Return interrupt status
Input Parameters	*ct: pointer to CTIM

Return Value	Int status
Condition	No
Function called	-

*****************************************************************************/
uint8_t CTIM_GetIntStatus(CTIM0_Type *ct)
{
	return (uint8_t)ct->IR;
}
/*****************************************************************************
Function Name	CTIM_PWMInit
Function Definition	void CTIM_PWMInit(CTIM0_Type *ct, uint32_t pwmfreqinhz)
Function Description	Initial Timer 1 output PWM wave
Input Parameters	*ct: pointer to CTIM
pwmfreqinhz: PWM frequency
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_PWMInit(CTIM0_Type *ct, uint32_t pwmfreqinhz)
{
	uint32_t pwmmod;
	pwmmod=SystemCoreClock/((ct->PR_b.PCVAL+1)*pwmfreqinhz);
	CTIM_ConfigMatch3(ct,pwmmod,TIM_MATCH_TRIGGER_INT|TIM_MATCH_RESET_COUNTER,TIM_MATCH_OUT_DO_NOTHING); 
}
/*****************************************************************************
Function Name	CTIM_EnablePWMOutput
Function Definition	void CTIM_EnablePWMOutput(CTIM0_Type *ct, uint8_t pwmchannels)
Function Description	Setup output PWM channels
Input Parameters	*ct: pointer to CTIM
pwmchannels: PWM channels
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_EnablePWMOutput(CTIM0_Type *ct, uint8_t pwmchannels)
{
	ct->PWMC |= (pwmchannels|1); //PWM0 must be enable
	
	return;
}
/*****************************************************************************
Function Name	CTIM_DisablePWM
Function Definition	void CTIM_DisablePWM(CTIM0_Type *ct, uint8_t pwmchannels)
Function Description	Disable PWM channels output
Input Parameters	*ct: pointer to CTIM
pwmchannels: PWM channels
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_DisablePWM(CTIM0_Type *ct, uint8_t pwmchannels)
{
	ct->PWMC &= ~pwmchannels;
	
	return;
}
/*****************************************************************************
Function Name	CTIM_SetPWM0Duty
Function Definition	void CTIM_SetPWM0Duty(CTIM0_Type *ct, uint8_t duty)
Function Description	Setup PWM duty at MAT0 
Input Parameters	*ct: pointer to CTIM
duty: PWM duty, 0~100%
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_SetPWM0Duty(CTIM0_Type *ct, uint8_t duty)
{
	if (duty>100) return;
	if (duty==0)
		ct->MR0_b.MATCH=ct->MR3_b.MATCH+1;
	else if (duty==100)
		ct->MR0_b.MATCH=0;
	else
		ct->MR0_b.MATCH=ct->MR3_b.MATCH*(100-duty)/100-1;
}
/*****************************************************************************
Function Name	CTIM_SetPWM1Duty
Function Definition	void CTIM_SetPWM1Duty(CTIM0_Type *ct, uint8_t duty)
Function Description	Setup PWM duty at MAT1 
Input Parameters	*ct: pointer to CTIM
duty: PWM duty, 0~100%
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_SetPWM1Duty(CTIM0_Type *ct, uint8_t duty)
{
	if (duty>100) return;
	if (duty==0)
		ct->MR1_b.MATCH=ct->MR3_b.MATCH+1;
	else if (duty==100)
		ct->MR1_b.MATCH=0;
	else
		ct->MR1_b.MATCH=ct->MR3_b.MATCH*(100-duty)/100-1;
}
/*****************************************************************************
Function Name	CTIM_SetPWM2Duty
Function Definition	void CTIM_SetPWM2Duty(CTIM0_Type *ct, uint8_t duty)
Function Description	Setup PWM duty at MAT2 
Input Parameters	*ct: pointer to CTIM
duty: PWM duty, 0~100%
Return Value	No
Condition	No
Function called	-

*****************************************************************************/
void CTIM_SetPWM2Duty(CTIM0_Type *ct, uint8_t duty)
{
	if (duty>100) return;
	if (duty==0)
		ct->MR2_b.MATCH=ct->MR3_b.MATCH+1;
	else if (duty==100)
		ct->MR2_b.MATCH=0;
	else
		ct->MR2_b.MATCH=ct->MR3_b.MATCH*(100-duty)/100-1;
}

